package com.sdu.shanyimisapi.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.sdu.shanyimisapi.common.PageUtils;
import com.sdu.shanyimisapi.common.R;
import com.sdu.shanyimisapi.controller.form.DeleteAppUserForm;
import com.sdu.shanyimisapi.controller.form.InsertOrUpdateAppUserForm;
import com.sdu.shanyimisapi.controller.form.SearchAppUserByIdForm;
import com.sdu.shanyimisapi.controller.form.SearchAppUserByPageForm;
import com.sdu.shanyimisapi.service.AppUserService;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lihaopeng
 * @version 1.0
 * @project shanyi-mis-api
 * @description App用户
 * @since 2024/4/13 22:33
 */
@RestController
@RequestMapping("/app_user")
public class AppUserController {

    @Resource
    private AppUserService appUserService;

    /**
     * 分页查询App用户
     *
     * @param form
     * @return
     */
    @PostMapping("/searchByPage")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "APP_USER:SELECT"}, mode = SaMode.OR)
    public R searchByPage(@RequestBody @Valid SearchAppUserByPageForm form) {
        Map param = BeanUtil.beanToMap(form);
        int page = form.getPage();
        int length = form.getLength();
        int start = (page - 1) * length;
        param.put("start", start);
        PageUtils pageUtils = appUserService.searchByPage(param);
        return R.ok().put("result", pageUtils);
    }

    /**
     * 根据id查询App用户
     *
     * @param form
     * @return
     */
    @PostMapping("/searchById")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "APP_USER:SELECT"}, mode = SaMode.OR)
    public R searchById(@RequestBody @Valid SearchAppUserByIdForm form) {
        HashMap map = appUserService.searchById(form.getId());
        return R.ok(map);
    }

    /**
     * 更新App用户头像
     *
     * @param file
     * @param id
     * @return
     */
    @PostMapping("/updateAvatar")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "APP_USER:UPDATE"}, mode = SaMode.OR)
    public R updateAvatar(@Param("file") MultipartFile file, @Param("id") Integer id) {
        appUserService.updateAvatar(file, id);
        return R.ok();
    }

    /**
     * 新增App用户
     *
     * @param form
     * @return
     */
    @PostMapping("/insertAppUser")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "APP_USER:INSERT"}, mode = SaMode.OR)
    public R insertAppUser(@RequestBody @Valid InsertOrUpdateAppUserForm form) {
        Map param = BeanUtil.beanToMap(form);
        String json = JSONUtil.parseArray(form.getLikes()).toString();
        param.replace("likes", json);
        json = JSONUtil.parseArray(form.getDislikes()).toString();
        param.replace("dislikes", json);
        appUserService.insertAppUser(param);
        return R.ok();
    }

    /**
     * 更新App用户
     *
     * @param form
     * @return
     */
    @PostMapping("/updateAppUser")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "APP_USER:UPDATE"}, mode = SaMode.OR)
    public R updateAppUser(@RequestBody @Valid InsertOrUpdateAppUserForm form) {
        Map param = BeanUtil.beanToMap(form);
        String json = JSONUtil.parseArray(form.getLikes()).toString();
        param.replace("likes", json);
        json = JSONUtil.parseArray(form.getDislikes()).toString();
        param.replace("dislikes", json);
        appUserService.updateAppUser(param);
        return R.ok();
    }

    /**
     * 删除App用户
     *
     * @param form
     * @return
     */
    @PostMapping("/deleteAppUser")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "APP_USER:DELETE"}, mode = SaMode.OR)
    public R deleteAppUser(@RequestBody @Valid DeleteAppUserForm form) {
        appUserService.deleteAppUser(form.getIds());
        return R.ok();
    }

}
